/**
 * 
 */
package com.nh.action;

import java.sql.Timestamp;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;

import com.nh.entity.NgAdmin;
import com.nh.entity.NgAuthorization;

/**
 * 项目名称：NotoginsengManage
 * 
 * 类名称：AdminAction
 * 
 * 类描述： 管理员管理
 * 
 * 创建人：李兴建
 * 
 * E-mail:lxj08193@163.com
 * 
 * 创建时间：2012-11-2 上午12:11:26
 * 
 * 修改人：李兴建
 * 
 * 修改时间：2012-11-2 上午12:11:26
 * 
 * 修改备注：
 * 
 * @version
 */
public class AdminAction extends BaseAction {
	private NgAdmin admin;
	private Long optionId;
	private NgAuthorization authorization;

	private String oldPassword;
	private String newPassword1;
	private String newPaasword2;

	/**
	 * 添加管理员页面
	 * 
	 * @return
	 */
	public String addAdminPage() {
		admin = new NgAdmin();
		admin.setAdminType(0);
		admin.setState(0);
		return "addAdminPage";
	}

	/**
	 * 保存添加
	 * 
	 * @return
	 */
	public String addAdmin() {
		if (StringUtils.isEmpty(admin.getRealEname())) {
			this.addFieldError("realNameError", "名称格式不正确");
			return "addAdminPage";
		}
		if (StringUtils.isEmpty(admin.getLoginName())) {
			this.addFieldError("loginNameError", "登录名格式不正确");
			return "addAdminPage";
		}
		if (StringUtils.isEmpty(admin.getLoginPassword())) {
			this.addFieldError("loginPasswordError", "密码格式不正确");
			return "addAdminPage";
		}

		if (this.getBaseService().findByProperty("loginName", admin.getLoginName(), NgAdmin.class).size() > 0) {
			this.addFieldError("loginNameError", "登录名已经存在");
			return "addAdminPage";
		}

		admin.setCreateadmin(this.getCurrentAdmin().getRealEname());
		admin.setCreateTime(new Timestamp(System.currentTimeMillis()));
		this.getBaseService().save(admin);

		// 添加权限
		if (admin.getAdminType() == 0) {
			this.getBaseService().save(getAdminDefultAuthorization(admin.getId()));
		} else if (admin.getAdminType() == 1) {
			//this.getBaseService().save(getSuperAdminDefultAuthorization(admin.getId()));
		} else if (admin.getAdminType() == 2) {
			this.getBaseService().save(getExpertDefultAuthorization(admin.getId()));
		}

		return adminListPage();
	}

	/**
	 * 管理员列表
	 * 
	 * @return
	 */
	public String adminListPage() {
		Criteria criteria = this.createCriteria(NgAdmin.class);
		criteria.addOrder(Order.desc("adminType"));
		this.queryByPage(criteria, "adminAction!adminListPage.action", 10);
		return "adminListPage";
	}

	/**
	 * 修改管理员页面
	 * 
	 * @return
	 */
	public String modifyAdminPage() {
		admin = this.getBaseService().findById(optionId, NgAdmin.class);
		return "modifyAdminPage";
	}

	/**
	 * 保存修改管理员
	 * 
	 * @return
	 */
	public String savemodifyAdmin() {
		if (StringUtils.isEmpty(admin.getRealEname())) {
			this.addFieldError("realNameError", "名称格式不正确");
			return "modifyAdminPage";
		}
		if (StringUtils.isEmpty(admin.getLoginName())) {
			this.addFieldError("loginNameError", "登录名格式不正确");
			return "modifyAdminPage";
		}
		if (StringUtils.isEmpty(admin.getLoginPassword())) {
			this.addFieldError("loginPasswordError", "密码格式不正确");
			return "modifyAdminPage";
		}
		admin.setCreateadmin("0");
		admin.setCreateTime(new Timestamp(System.currentTimeMillis()));
		this.getBaseService().saveOrUpdate(admin, NgAdmin.class);

		return adminListPage();
	}

	/**
	 * 停用
	 * 
	 * @return
	 */
	public String stopAdmin() {
		admin = this.getBaseService().findById(optionId, NgAdmin.class);
		if (admin.getState() == 0) {
			admin.setState(1);
			this.getBaseService().saveOrUpdate(admin, NgAdmin.class);
		}
		return savemodifyAdmin();
	}

	/**
	 * 启用
	 * 
	 * @return
	 */
	public String startAdmin() {
		admin = this.getBaseService().findById(optionId, NgAdmin.class);
		if (admin.getState() == 1) {
			admin.setState(0);
			this.getBaseService().saveOrUpdate(admin, NgAdmin.class);
		}
		return savemodifyAdmin();
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	public String remove() {
		admin = this.getBaseService().findById(optionId, NgAdmin.class);
		admin.setState(2);
		this.getBaseService().saveOrUpdate(admin, NgAdmin.class);
		return savemodifyAdmin();
	}

	/**
	 * 恢复
	 * 
	 * @return
	 */
	public String reRemove() {
		admin = this.getBaseService().findById(optionId, NgAdmin.class);
		if (admin.getState() == 2) {
			admin.setState(0);
			this.getBaseService().saveOrUpdate(admin, NgAdmin.class);
		}
		return savemodifyAdmin();
	}

	/**
	 * 设置密码页面
	 * 
	 * @return
	 */
	public String resetPasswordPage() {
		oldPassword = "";
		newPassword1 = "";
		newPaasword2 = "";

		return "resetPasswordPage";
	}

	/**
	 * 权限分配页面
	 * 
	 * @return
	 */
	public String authorizationPage() {
		admin = this.getBaseService().findById(optionId, NgAdmin.class);
		List<NgAuthorization> list = this.getBaseService().findByProperty("adminId", admin.getId(), NgAuthorization.class);
		if (list.size() > 0) {
			authorization = list.get(0);
		}
		return "authorizationPage";
	}

	/**
	 * 保存权限
	 * 
	 * @return
	 */
	public String authorizationSave() {
		this.getBaseService().saveOrUpdate(authorization, NgAuthorization.class);
		return adminListPage();
	}

	/**
	 * 密码保存
	 * 
	 * @return
	 */
	public String savePassword() {
		if (StringUtils.isEmpty(oldPassword)) {
			this.addFieldError("Password1Error", "旧密码格式不正确");
			return "resetPasswordPage";
		}
		if (StringUtils.isEmpty(newPassword1)) {
			this.addFieldError("Password2Error", "新密码格式不正确");
			return "resetPasswordPage";
		}
		if (StringUtils.isEmpty(newPaasword2)) {
			this.addFieldError("Password3Error", "确认密码格式不正确");
			return "resetPasswordPage";
		}
		if (!newPassword1.equals(newPaasword2)) {
			this.addFieldError("Password3Error", "两次密码输入不同");
			return "resetPasswordPage";
		}
		NgAdmin admin = this.getCurrentAdmin();
		if (oldPassword.equals(admin.getLoginPassword())) {
			admin.setLoginPassword(newPassword1);
			this.getBaseService().saveOrUpdate(admin, NgAdmin.class);
			this.addFieldError("Password4Error", "密码修改成功");
		} else {
			this.addFieldError("Password1Error", "密码不正确");
		}

		return "resetPasswordPage";
	}

	/**
	 * 超级管理员默认权限
	 * 
	 * @param adminId
	 * @return
	 */
	public static NgAuthorization getSuperAdminDefultAuthorization(Long adminId) {
		NgAuthorization authorization = new NgAuthorization();
		authorization.setAdminId(adminId);

		authorization.setAuth1(true);
		authorization.setAuth12(true);
		authorization.setAuth13(true);
		authorization.setAuth14(true);
		authorization.setAuth15(true);
		authorization.setAuth16(true);
		authorization.setAuth17(true);
		authorization.setAuth18(true);

		authorization.setAuth2(true);
		authorization.setAuth21(true);
		authorization.setAuth22(true);
		authorization.setAuth23(true);
		authorization.setAuth24(true);

		authorization.setAuth7(true);
		authorization.setAuth71(true);
		authorization.setAuth72(true);

		authorization.setAuth3(true);
		authorization.setAuth31(true);
		authorization.setAuth32(true);
		authorization.setAuth33(true);
		authorization.setAuth34(true);

		authorization.setAuth4(true);
		authorization.setAuth41(true);
		authorization.setAuth42(true);
		authorization.setAuth43(true);
		authorization.setAuth44(true);

		authorization.setAuth5(true);
		authorization.setAuth51(true);
		authorization.setAuth52(true);
		authorization.setAuth53(true);

		authorization.setAuth6(true);
		authorization.setAuth61(true);
		authorization.setAuth62(true);

		return authorization;
	}

	/**
	 * 普通超级管理员默认权限
	 * 
	 * @param adminId
	 * @return
	 */
	public static NgAuthorization getAdminDefultAuthorization(Long adminId) {
		NgAuthorization authorization = new NgAuthorization();
		authorization.setAdminId(adminId);

		authorization.setAuth1(true);
		authorization.setAuth12(true);
		authorization.setAuth13(true);
		authorization.setAuth14(true);
		authorization.setAuth15(true);
		authorization.setAuth16(true);
		authorization.setAuth17(true);
		authorization.setAuth18(true);

		authorization.setAuth2(true);
		authorization.setAuth21(true);
		authorization.setAuth22(true);
		authorization.setAuth23(true);
		authorization.setAuth24(true);

		authorization.setAuth7(true);
		authorization.setAuth71(true);
		authorization.setAuth72(true);

		authorization.setAuth3(true);
		authorization.setAuth31(true);
		authorization.setAuth32(true);
		authorization.setAuth33(true);
		authorization.setAuth34(true);

		authorization.setAuth4(true);
		authorization.setAuth41(false);
		authorization.setAuth42(false);
		authorization.setAuth43(false);
		authorization.setAuth44(true);

		authorization.setAuth5(true);
		authorization.setAuth51(true);
		authorization.setAuth52(true);
		authorization.setAuth53(true);

		authorization.setAuth6(true);
		authorization.setAuth61(true);
		authorization.setAuth62(true);

		return authorization;
	}

	/**
	 * 专家默认权限
	 * 
	 * @param adminId
	 * @return
	 */
	public static NgAuthorization getExpertDefultAuthorization(Long adminId) {
		NgAuthorization authorization = new NgAuthorization();
		authorization.setAdminId(adminId);

		authorization.setAuth1(true);
		authorization.setAuth12(true);
		authorization.setAuth13(false);
		authorization.setAuth14(false);
		authorization.setAuth15(false);
		authorization.setAuth16(false);
		authorization.setAuth17(false);
		authorization.setAuth18(false);

		authorization.setAuth2(true);
		authorization.setAuth21(false);
		authorization.setAuth22(false);
		authorization.setAuth23(true);
		authorization.setAuth24(false);

		authorization.setAuth7(true);
		authorization.setAuth71(true);
		authorization.setAuth72(true);

		authorization.setAuth3(true);
		authorization.setAuth31(true);
		authorization.setAuth32(true);
		authorization.setAuth33(true);
		authorization.setAuth34(true);

		authorization.setAuth4(false);
		authorization.setAuth41(false);
		authorization.setAuth42(false);
		authorization.setAuth43(false);
		authorization.setAuth44(false);

		authorization.setAuth5(false);
		authorization.setAuth51(false);
		authorization.setAuth52(false);
		authorization.setAuth53(false);

		authorization.setAuth6(false);
		authorization.setAuth61(false);
		authorization.setAuth62(false);

		return authorization;
	}

	/**
	 * @return the admin
	 */
	public NgAdmin getAdmin() {
		return admin;
	}

	/**
	 * @param admin
	 *            the admin to set
	 */
	public void setAdmin(NgAdmin admin) {
		this.admin = admin;
	}

	/**
	 * @return the optionId
	 */
	public Long getOptionId() {
		return optionId;
	}

	/**
	 * @param optionId
	 *            the optionId to set
	 */
	public void setOptionId(Long optionId) {
		this.optionId = optionId;
	}

	/**
	 * @return the authorization
	 */
	public NgAuthorization getAuthorization() {
		return authorization;
	}

	/**
	 * @param authorization
	 *            the authorization to set
	 */
	public void setAuthorization(NgAuthorization authorization) {
		this.authorization = authorization;
	}

	/**
	 * @return the oldPassword
	 */
	public String getOldPassword() {
		return oldPassword;
	}

	/**
	 * @param oldPassword
	 *            the oldPassword to set
	 */
	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	/**
	 * @return the newPassword1
	 */
	public String getNewPassword1() {
		return newPassword1;
	}

	/**
	 * @param newPassword1
	 *            the newPassword1 to set
	 */
	public void setNewPassword1(String newPassword1) {
		this.newPassword1 = newPassword1;
	}

	/**
	 * @return the newPaasword2
	 */
	public String getNewPaasword2() {
		return newPaasword2;
	}

	/**
	 * @param newPaasword2
	 *            the newPaasword2 to set
	 */
	public void setNewPaasword2(String newPaasword2) {
		this.newPaasword2 = newPaasword2;
	}

}
